<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_Controller extends MY_Controller {


	protected $section = null;
	public $action = array();//toolbar
//	public $is_page_admin = true;
	public $lang_array_select = array();
	public $config_module;
	
	//modules
	public $module_top;
	public $module_left;
	public $module_orther;
	
	public $access;
	public function __construct()
	{
		parent::__construct();
		$this->config->set_item('language',$this->lang_current);
		$this->load->language('admin',$this->lang_current);
		$this->load->helper('admin_theme');
		
		$this->template->set_theme('admin');
		
		//Asset::add_path('theme', $this->template->get_theme_path());
		//Asset::set_path('theme');
		

		if ($this->template->layout_exists('default.php'))
		{
			$this->template->set_layout('default.php');
		}
		
		// Template configuration
		//$this->template
			//->enable_parser(false)
			//->set('theme_options', $this->theme_options)
		//	->set_theme('admin')//ADMIN_THEME
		//	->set_layout('default', 'admin');

		// trigger the run() method in the selected admin theme
//		$class = 'Theme_'.ucfirst('admin');//$this->admin_theme->slug
		//call_user_func(array(new $class, 'run'));
		//ci()->action = $this->action;
		
		if($this->module){
			//load base controller cho module
			include_once(APPPATH.'modules/admin.php');
			$base = new admin($this->module);
//			if($this->method
			$this->config_module = $base->config_module;

		}
		
		$this->_check_access();
		$this->load->helper('admin');
		$href_return = "javascript:void(0)";
		if($this->method == "index"){
			$this->action = 'new|'.site_url($this->uri->uri_string()).'/create|'.lang('global:addnew').'+ remove|'.$href_return.'|'.lang('global:delete_selected');//class|action|title
			//$this->action = 'new|create|'.lang('global:addnew').'+ remove|'.$href_return.'|'.lang('global:delete_selected');//class|action|title
			
			//la trang su dung table
			$this->template->append_css('table.css');
			/*->append_js('data-tables/jquery.dataTables.js')
			->append_css('data-tables/jquery.dataTables.css');*/
		}
		elseif($this->method == "create" || $this->method == "edit"){
			$this->action = 'save|'.$href_return.'|'.lang('global:save')  
						.'+ savenew|'.$href_return.'|'.lang('global:save_new') 
						.'+ saveclose|'.$href_return.'|'.lang('global:save_close')  
						.'+ back|'.$href_return.'|'.lang('global:back');//class|action|title
			$this->template
			->set('action',$this->action)
			->append_css('error.css')
			->append_js('jquery/jquery.slugify.js')
			->append_js('ckeditor/ckeditor.js');
			
		}
		
		//active menu left
		$this->template->append_metadata('<style>#leftPanel li.'.$this->module.' > a{background:#D8DFEA}</style>');
		//echo $this->module;
		//LAY MODULE THEO QUYEN
		if(!empty($this->current_user)){
			//if( $this->current_user->group_id != 1 ){//chi co admin moi dc vao trang admin
			//	$this->check_access('/');
			//}
			//luu cache
			$this->load->library('kunkuncache');
			$path = 'permissions/groups_top_'.$this->current_user->group_id;
			if(!$this->module_top = $this->kunkuncache->get($path)){
				$this->module_top = $this->permission_m->get_module($this->current_user->group_id,$sys = 0,$status = 1);
				$this->kunkuncache->write($this->module_top, $path);
			}
			$path = 'permissions/groups_left_'.$this->current_user->group_id;
			if(!$this->module_left = $this->kunkuncache->get($path)){
				$this->module_left = $this->permission_m->get_module($this->current_user->group_id,$sys = array('1,2'),$status = 1);
				//xu ly menu cua module
				foreach($this->module_left as $item){
					if (file_exists(APPPATH.'modules/'.$item->module.'/config/config.php'))
					{
						$this->config->load($item->module.'/config');
						$item->admin_menu = $this->config->item('admin_menu');
						$item->title = $this->config->item('title');
					}
				}
				$this->kunkuncache->write($this->module_left, $path);
			}
			$this->module_orther = array();
			foreach($this->module_left as $o=>$values){
				if($values->system==2){
					$this->module_orther[] = $values;
					unset($this->module_left[$o]);
				}
				
					
			}
			
			//neu ko co module nao thi ko cho vao admin lun
			$out = true;
			if(count($this->module_top)==0 && count($this->module_left)==0)
				$out = false;
			//if(count($this->module_orther)==0)
			//	$out = false;
			if(empty($out)){
				$this->check_access('/');
			}
			$this->template->set('module_top',$this->module_top);
			$this->template->set('module_left',$this->module_left);
			$this->template->set('module_orther',$this->module_orther);
			$this->template->set('user',$this->current_user);
			$this->render_modules($this->module_left);
			$this->render_modules($this->module_orther);
			//$this->check_permissions();			
		}
		//error message
		$this->template->set('error',$this->session->flashdata('error'));
		//print_r($this->current_user);exit;
		//print_r($module_top);exit;
	}

	//render and load language of list module
	private function render_modules($modules){
		foreach($modules as $item)
			if (file_exists(APPPATH.'modules/'.$item->module.'/language/'.$this->lang_current.'/'.$item->module.'_lang.php'))
				$this->load->language($item->module.'/'.$item->module,$this->lang_current);
	}
	/**
	 * Checks to see if a user object has access rights to the admin area.
	 *
	 * @return boolean 
	 */
	private function _check_access()
	{
		// These pages get past permission checks
		$ignored_pages = array('admin/login', 'admin/logout', 'admin/help');

		// Check if the current page is to be ignored
		$current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, 'index');

		// Dont need to log in, this is an open page
		if (in_array($current_page, $ignored_pages))
		{
			return true;
		}

		if ( ! $this->current_user)
		{
			// save the location they were trying to get to
			$this->session->set_userdata('admin_redirect', $this->uri->uri_string());
			redirect('admin/login','refresh');
		}

		// Admins can go straight in
		if ($this->current_user->group === 'admin')
		{
			return true;
		}

		// Well they at least better have permissions!
		if ($this->current_user)
		{
			// We are looking at the index page. Show it if they have ANY admin access at all
			if ($current_page == 'admin/index' && $this->permissions)
			{
				return true;
			}

			// Check if the current user can view that page
			return array_key_exists($this->module, $this->permissions);
		}

		// god knows what this is... erm...
		return false;
	}
	
	function view($data = array()){ // view form
		$this->template
			->set($data)
			->append_js('jquery.tabify.js')
			->build($this->controller.'/form');
	}
	
	function redirect($method,$type,$id){
		if($type == 'save')
			redirect('admin/'.$this->module);
		if($type == 'add')
			redirect('admin/'.$this->module.'/'.$method);
		if($type == 'edit')
			redirect('admin/'.$this->module.'/edit/'.$id);
	}
	function redirect_to_action($method,$action,$type,$id){
		if($type == 'save')
			redirect('admin/'.$this->module.'/'.$action);
		if($type == 'add')
			redirect('admin/'.$this->module.'/'.$action.'/'.$method);
		if($type == 'edit')
			redirect('admin/'.$this->module.'/'.$action.'/edit/'.$id);
	}
	
	function check_access($redirect = NULL){
		$_redirect = empty($redirect) ? 'admin' : '';
		$this->session->set_flashdata('error', lang('global:access-denied'));
		redirect($_redirect);
	}
	//neu user nhap url vao nhung vung ko cho phep
	public function check_permissions(){
			$module = $this->module;
			$per = array();
			if(!empty($module)){
				foreach($this->module_orther as $mod){
					$m = (array) $mod;
					$per[] = $m['module'];
				}
				foreach($this->module_top as $mode){
					$m = (array) $mod;
					$per[] = $m['module'];
				}
				foreach($this->module_left as $mod){
					$m = (array) $mod;
					$per[] = $m['module'];
				}
				
				if(!in_array($module,$per))
					$this->check_access();
			}
	}

}